//
//  ByUIShopsNewIndexVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/10/10.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIShopsNewIndexVC.h"

// vc
#import "ByUIShopsIndexVC.h"
// view
#import "ByViewShopsIndexHeader.h"
#import "ByViewClassificationScroll.h"      // 分类滑动视图
#import "ByViewSubClassificationScroll.h"   // 子分类滑动视图
// api
#import "ByApiShops.h"
@interface ByUIShopsNewIndexVC ()
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *imgs;
@property (nonatomic, strong) NSArray *unImgs;
@property (nonatomic, strong) NSArray *colors;

@property (nonatomic, strong) ByViewShopsIndexHeader        *headerView;
@property (nonatomic, strong) ByViewClassificationScroll    *classScroll;
@property (nonatomic, strong) ByViewSubClassificationScroll *subClassScroll;

@property (nonatomic, strong) NSArray                       *dataArray;             // 第一级数据
@end

@implementation ByUIShopsNewIndexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"联盟商家";
    [self addBYItemviews:@[@"全部"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    [self addLeftBackBarButtonItem];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setValue:self.colors[0] forKey:@"select_color"];
    [userDefault synchronize];
    [self getCates];
}


// getter
- (NSArray *)titles{
    if (_titles == nil) {
        _titles = @[@"美食",@"休闲娱乐",@"生活服务",@"旅行"];
    }
    return _titles;
}

- (NSArray *)imgs{
    if (_imgs == nil) {
        _imgs = @[@"Shops_meishi",@"Shops_xiuxianyule",@"Shops_shenghuofuwu",@"Shops_lvxing"];
    }
    return _imgs;
}

- (NSArray *)unImgs{
    if (_unImgs == nil) {
        _unImgs = @[@"Shops_unmeishi",@"Shops_unxxyl",@"Shops_unshfw",@"Shops_unlx"];
    }
    return _unImgs;
}

- (NSArray *)colors{
    if (!_colors) {
//        _colors = @[[UIColor colorWithR:107 g:189 b:240],[UIColor colorWithR:217 g:191 b:87],[UIColor colorWithR:177 g:136 b:133],[UIColor colorWithR:209 g:133 b:138]];
        _colors = @[@[@"107",@"189",@"240"],@[@"240",@"191",@"87"],@[@"177",@"136",@"231"],@[@"209",@"233",@"138"]];
    }
    return _colors;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (ByViewClassificationScroll *)classScroll {
    if (!_classScroll) {
        CGFloat width = [@"各地特色菜" getSizeOfString:14].width + 10 + 15 * 2 + 10 + [@"111" getSizeOfString:14].width;  // + 10(宽度) + 15 * 2 + 10(间距) + count_width
        _classScroll = [[ByViewClassificationScroll alloc] initWithFrame:CGRectMake(10, _headerView.bottom + 10, width, SCREEN_HEIGHT - 64 - 120 - 10) style:UITableViewStylePlain];
        [self.view addSubview:_classScroll];
        if (@available(iOS 11.0,*)) {
            _classScroll.height = self.view.height - 120 - 10 - self.view.safeAreaInsets.bottom - self.view.safeAreaInsets.top;
        }else{
            _classScroll.height = SCREEN_HEIGHT - 64 - 120 - 10;
        }
    }
    return _classScroll;
}
- (ByViewSubClassificationScroll *)subClassScroll {
    if (!_subClassScroll) {
        CGFloat width = [@"各地特色菜" getSizeOfString:14].width + 10 + 15 * 2 + 10 + [@"111" getSizeOfString:14].width;
        _subClassScroll = [[ByViewSubClassificationScroll alloc] initWithFrame:CGRectMake(width + 10, _headerView.bottom + 10, SCREEN_WIDTH - width - 20, SCREEN_HEIGHT - 64  - 120 - 10) style:UITableViewStylePlain];
        [self.view addSubview:_subClassScroll];
        NSLog(@"%lf-----%lf",SCREEN_WIDTH - width - 20,SCREEN_WIDTH);
        if (@available(iOS 11.0,*)) {
            _subClassScroll.height = self.view.height - 120 - 10 -self.view.safeAreaInsets.bottom - self.view.safeAreaInsets.top;
        }else{
            _subClassScroll.height = SCREEN_HEIGHT - 64 - 120 - 10;
        }
        
    }
    return _subClassScroll;
}


// 创建视图
- (void)createHeaderView{
    _headerView = [[ByViewShopsIndexHeader alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 120) andTitles:self.titles andImgs:self.imgs andUnImgs:self.unImgs];
    __weak typeof(self) weakSelf = self;
    _headerView.selectBlock = ^(NSInteger selectIndex) {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setValue:weakSelf.colors[selectIndex - 10] forKey:@"select_color"];
        [userDefault synchronize];
        [weakSelf changeCates:weakSelf.dataArray[selectIndex - 10]];
        
    };
    [self.view addSubview:_headerView];
}

-(void)rightItemsAction:(NSInteger)rightIndex{
    ByUIShopsIndexVC *vc = [[ByUIShopsIndexVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}


#pragma mark -- 类目
// 获取数据
- (void)getCates {
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    [ByApiShops getShopCate:model success:^(ByNetBaseResult *result) {
        if ([result.data isKindOfClass:[NSArray class]]) {
            self.dataArray = result.data;
        }
        [self createHeaderView];
        // 设置初始化数据
        ByModelShopCate *one_model = self.dataArray[0];
        NSMutableArray *temp = [NSMutableArray array];
        for (ByModelShopCate *sub_model in one_model.children) {
            [temp addObject:sub_model];
        }
        self.classScroll.classModels = [temp copy];
        ByModelShopCate *first_model = temp[0];
        [self getProductList:first_model];
        // 设置点击回调
        [self setScrollClickBlock];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
    
}

- (void)changeCates:(ByModelShopCate *)model{

    // 设置初始化数据
    NSLog(@"%@",model);
    NSMutableArray *temp = [NSMutableArray array];
    for (ByModelShopCate *sub_model in model.children) {
        [temp addObject:sub_model];
    }
    if (temp.count != 0) {
        self.classScroll.hidden = NO;
        self.classScroll.classModels = [temp copy];
        ByModelShopCate *first_model = temp[0];
        [self getProductList:first_model];
        self.classScroll.selectingIndex = 0;
        [self.classScroll reloadData];
    }else{
        self.classScroll.hidden = YES;
    }

}

// 列表
- (void)getProductList:(ByModelShopCate *)model{
    NSMutableArray *temp = [NSMutableArray array];
    for (ByModelShopCate *sub_model in model.children) {
        [temp addObject:sub_model];
    }
    if (temp.count != 0) {
        self.subClassScroll.hidden = NO;
        self.subClassScroll.itemModels = [temp copy];
        [self.subClassScroll reloadData];
    }else{
        self.subClassScroll.hidden = YES;
    }

}


#pragma mark - 设置点击回调
- (void)setScrollClickBlock {
    __weak typeof(self) weakself = self;
    [self.classScroll setSelectBlock:^(ByModelShopCate *model) {
        [weakself getProductList:model];
    }];
    
    
}

@end
